package com.atwl.school_system.controller;

import com.atwl.school_system.service.UserService;
import com.atwl.school_system.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/user/login")
    public R login(@RequestParam("phone") String phone,
                   @RequestParam("password") String password) {
        return userService.login(phone, password);
    }

    /*
     private Integer id;
    private String userName;
    private Integer userId;
    private String password;
    private Date enrollTime;
     */
    @PostMapping("/user/register")
    public R register(@RequestParam("phone") String phone,
                      @RequestParam("password") String password,
                      @RequestParam("code") String code) {
        return userService.register(phone, password, code);
    }

    @GetMapping("/user/code")
    public R getCode() {
        String code = UUID.randomUUID().toString().substring(0, 6);
        stringRedisTemplate.opsForValue().set("code", code);
        return R.ok().setData(code);
    }
}
